import { SUCCESS } from '../../../../yali-sdk/utils/success';
import frequentClick from '../../../../yali-sdk/utils/frequentClick';
import apiGetLiveFollowList from '../../../../yali-sdk/business/live/api/apiGetLiveFollowList';
import  {filterImg}  from '../../../imgBaseUrl';
import { FAIL } from '../../../../yali-sdk/utils/fail';
import getIn from '../../../../yali-sdk/utils/getIn';
Page({

  /**
   * 页面的初始数据
  */
  data: {
    anchorList:[],
    params: {
      page: 1,
      rows: 10,
      follow_username:''
    }
  },
   /**
   * 生命周期函数--监听页面加载
   */
  onLoad (options) {
    const anchor_id =options.anchor_id;
    this.setData({
      'params.anchor_id':anchor_id
    })
    this.fecthData();
  },
  fecthData() {
    wx.showLoading();
    this.setData({ loading: true });
    const params = { ...this.data.params}
     global.actionDispatcher.execute(apiGetLiveFollowList,params, (status, result) => {
      if(status == SUCCESS) {
        const total = getIn(result.data, ['count']);
        let anchorList = getIn(result.data, ['list']);
          anchorList && anchorList.forEach(function(value,index,array) {
          anchorList[index] = filterImg(value,'head_photo');
         })
        anchorList = params.page == 1 ? anchorList : this.data.anchorList.concat(anchorList);
        this.setData({ anchorList, total, loading: false});
        wx.stopPullDownRefresh();
        wx.hideLoading()
      } else if (status == FAIL) {
        wx.hideLoading();
        wx.stopPullDownRefresh();
        this.setData({ loading: false });
      }
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh () {
    this.data.params = { ...this.data.params, page: 1 };
    this.fecthData()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom () {
    if (!loading && total > this.data.anchorList.length) {
      const page = this.data.params.page + 1;
      this.data.params = { ...this.data.params, page };
      this.fecthData()
    }
  },
  handleInputChange(options){
    let username = options.detail.value;
     this.setData({
      'params.follow_username':username
    })
    this.fecthData();
  },
  onFocus(options){
     let username = options.detail.value;
    this.setData({
      'params.follow_username':username
    })
     this.fecthData();
  },
  handleSearch(options){
    let username = this.data.params.follow_username?this.data.params.follow_username:'';
      this.setData({
      'params.follow_username':username
    })
  this.fecthData();
  }
})